package pos.MainMenu;

import java.util.*;
import javax.vecmath.Vector3d;
import pos.*;
import pos.MessageBox.MessageBox;
import pos.utils.IContainer;

/**
 * 主菜单模式控制器
 * @author plus
 */
public class MainMenuController implements IModeController{

    private HashMap<String, ICommand> CommandsPool = new HashMap<String, ICommand>();
    private static MainMenuSurrendings Surrendings;
    private static MainMenu Menu;
    
    public MainMenuController() {
        if (Surrendings==null || Menu==null)
            initObjects();
        BlendCommands();
    }

    public Object doEvent(String EventName, Object Param) {
        ICommand InvokedCommand=CommandsPool.get(EventName);
        if (InvokedCommand!=null)
            return InvokedCommand.execute(Param);
        else{
            System.err.println("Event:"+EventName+" NOT found!");
            return null;
        }
    }

    public void setMode() {
        ControlCenter.setView(
                new Vector3d(56,6,-20), new Vector3d(Math.PI*18.2/180,Math.PI*141.175/180,-Math.PI*10/180));
        ControlCenter.eventBehavior.setTarget(Menu, this);
        
        ControlCenter.objRoot.addChild(Surrendings);
        ControlCenter.dashBoardRoot.addChild(Menu);
        ControlCenter.preFrameInvoker.addLisener(Surrendings);
        ControlCenter.preFrameInvoker.addLisener(Menu);
    }
    
    
    public void disposeMode() {
        ControlCenter.preFrameInvoker.removeLisener(Surrendings);
        ControlCenter.preFrameInvoker.removeLisener(Menu);
        Surrendings.detach();
        //Menu.detach();
        ControlCenter.dashBoardRoot.removeAllChildren();
    }

    private void initObjects(){
        Surrendings=new MainMenuSurrendings();
        Menu=new MainMenu(this);
    }
    /**
     * 将事件名与Command对象绑定
     */
    private void BlendCommands() {
        CommandsPool.put("KeyEvent", new CommandKeyEvent((IContainer)Menu));
        CommandsPool.put("Enter", new CommandEnter(Menu));
        CommandsPool.put("ExitMsg", new pos.CommandMsgbox(
                new MessageBox(null,null,"退出","您确定要退出系统结束运营吗？",
                new String[]{"是的，回库收车","不,乘客还在下一站等我的车"},
                new String[]{"Exit","Cancel"})));
        CommandsPool.put("Exit", new CommandExit());
        CommandsPool.put("ChangeModeToConfig", new CommandChangeToConfig());
        CommandsPool.put("ChangeModeToDrive", new CommandChangeToDriving());
    }
}
